Bootstrap demo

Data Types in C Language

Comprehensive Reference Guide

Table of Contents

1. Introduction to Data Types

Data types in C specify the type of data that a variable can store. They define:

C language provides four main categories of data types:

2. Basic Data Types

Integer Types

Store whole numbers without decimal points.

Data Type Size (bytes) Format Specifier Example
int 2 or 4 %d int age = 25;
short 2 %hd short temp = -10;
long 4 or 8 %ld long population = 7800000000L;
long long 8 %lld long long big_num = 123456789012LL;

Floating-Point Types

Store real numbers with decimal points.

Data Type Size (bytes) Format Specifier Example
float 4 %f float pi = 3.14f;
double 8 %lf double precise = 3.1415926535;
long double 10-16 %Lf long double very_precise = 3.141592653589793L;

Character Type

Store single characters.

Data Type Size (bytes) Format Specifier Example
char 1 %c char grade = 'A';

Void Type

Represents no type or no value.

Data Type Usage Example
void Function return type void display() { }
void Function parameters int main(void) { }
void* Generic pointers void* ptr;

3. Derived Data Types

Arrays

Collection of elements of the same type.

// Integer array
int numbers[5] = {1, 2, 3, 4, 5};

// Character array (string)
char name[] = "John";

// 2D array
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};

Pointers

Variables that store memory addresses.

int num = 10;
int *ptr = #  // ptr stores address of num

char ch = 'A';
char *cptr = &ch;

Functions

Block of code that performs specific tasks.

// Function declaration
int add(int a, int b);

// Function definition
int add(int a, int b) {
    return a + b;
}

4. User-Defined Data Types

Structures

Group different data types together.

struct Student {
    int roll_no;
    char name[50];
    float marks;
};

// Usage
struct Student s1;
s1.roll_no = 101;
strcpy(s1.name, "Alice");
s1.marks = 85.5;

Unions

Similar to structures but share memory space.

union Data {
    int i;
    float f;
    char str[20];
};

// Usage
union Data data;
data.i = 10;  // Now only i has meaningful value

Enumerations

Define named integer constants.

enum Weekdays {
    MONDAY,    // 0
    TUESDAY,   // 1
    WEDNESDAY, // 2
    THURSDAY,  // 3
    FRIDAY,    // 4
    SATURDAY,  // 5
    SUNDAY     // 6
};

// Usage
enum Weekdays today = WEDNESDAY;

Typedef

Create aliases for existing types.

typedef unsigned int uint;
typedef struct {
    int x;
    int y;
} Point;

// Usage
uint count = 10;
Point p1 = {10, 20};

5. Type Modifiers

Signed Modifiers

signed int a = -10;    // Can be positive or negative
signed char b = -128;  // Range: -128 to 127

Unsigned Modifiers

unsigned int x = 40000;    // Only positive values
unsigned char y = 255;     // Range: 0 to 255

Short and Long Modifiers

short int s = 100;         // Smaller range
long int l = 1000000L;     // Larger range
long double ld = 3.14159L; // More precision

6. Type Qualifiers

Const

const int MAX_VALUE = 100;  // Cannot be modified
const float PI = 3.14159f;

Volatile

volatile int sensor_value;  // May change unexpectedly

Restrict

int* restrict ptr;  // No other pointer accesses the same memory

7. Type Conversion

Implicit Conversion (Automatic)

int i = 10;
float f = i;  // int to float conversion

char c = 'A';
int ascii = c;  // char to int conversion

Explicit Conversion (Casting)

float f = 3.14f;
int i = (int)f;  // f becomes 3

double d = 123.456;
int num = (int)d;  // d becomes 123

8. Size and Range of Data Types

Data Type Typical Size Range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 or 8 bytes -2,147,483,648 to 2,147,483,647 or larger
unsigned long 4 or 8 bytes 0 to 4,294,967,295 or larger
float 4 bytes 1.2E-38 to 3.4E+38
double 8 bytes 2.3E-308 to 1.7E+308
long double 10-16 bytes 3.4E-4932 to 1.1E+4932

Note: Sizes may vary depending on compiler and architecture.

9. Examples and Best Practices

Complete Example

#include 
#include 

// Structure example
struct Employee {
    int id;
    char name[50];
    float salary;
};

// Enumeration example
enum Status {
    ACTIVE,
    INACTIVE,
    SUSPENDED
};

int main() {
    // Basic types
    int age = 25;
    float height = 5.9f;
    char initial = 'J';
    
    // Array
    int scores[5] = {85, 90, 78, 92, 88};
    
    // Structure
    struct Employee emp1;
    emp1.id = 101;
    strcpy(emp1.name, "John Doe");
    emp1.salary = 50000.0f;
    
    // Enum
    enum Status user_status = ACTIVE;
    
    // Pointer
    int *age_ptr = &age;
    
    printf("Age: %d\n", age);
    printf("Employee Name: %s\n", emp1.name);
    printf("Status: %d\n", user_status);
    
    return 0;
}

Best Practices

  1. Choose appropriate data types for memory efficiency
  2. Use const for values that shouldn't change
  3. Be cautious with type conversions to avoid data loss
  4. Use typedef for complex types to improve readability
  5. Check data type sizes using sizeof() operator
  6. Use explicit casting when converting between types
  7. Initialize variables before use

Memory Management Tips

// Check size of data types
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of double: %zu bytes\n", sizeof(double));
printf("Size of char: %zu bytes\n", sizeof(char));

This comprehensive guide covers all fundamental aspects of data types in C programming, providing a solid foundation for understanding how data is stored and manipulated in the language.